/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dw2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Alberto
 */
public class HighscoreMenu extends JPanel implements ActionListener {
    private Image image;
    private JTextField[] names;
    private JTextField[] scores;
    private ImageButton quit;
    private Main parent;
    private boolean fromStart;

    public HighscoreMenu(Main parent, Image image, String[] nameStr, String[] scoreStr, Image quitImage) {
        this.parent = parent;
        this.image = image;
        fromStart = true;
        //setBackground(new Color(0, 255, 255));
        names = new JTextField[Main.numScores];
        scores = new JTextField[Main.numScores];
        Font titleFont = new Font(Font.SERIF, Font.PLAIN, 48);
        Font textFont = new Font(Font.SERIF, Font.PLAIN, 18);

        for(int i = 0; i < nameStr.length; i++) {
            names[i] = new JTextField(nameStr[i], 8);
            names[i].setFont(textFont);
            names[i].setEditable(false);
            names[i].setFocusable(false);
            names[i].setBorder(null);
            names[i].setOpaque(false);
            names[i].setActionCommand("" + i);
            names[i].setBackground(new Color(121, 88, 0));
            names[i].setForeground(Color.WHITE);
        }

        for(int i = 0; i < scoreStr.length; i++) {
            scores[i] = new JTextField(scoreStr[i], 8);
            scores[i].setFont(textFont);
            scores[i].setEditable(false);
            scores[i].setFocusable(false);
            scores[i].setBorder(null);
            scores[i].setOpaque(false);
            scores[i].setForeground(Color.WHITE);
        }

        setLayout(new BorderLayout());

        JPanel topPane = new JPanel();
        topPane.setOpaque(false);
        topPane.setLayout(new BoxLayout(topPane, BoxLayout.LINE_AXIS));

        JPanel titlePane = new JPanel();
        titlePane.setOpaque(false);
        titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.PAGE_AXIS));
        JLabel gameTitle = new JLabel("Deathworm Highscores");
        gameTitle.setFont(titleFont);
        gameTitle.setForeground(Color.WHITE);

        titlePane.add(gameTitle);
        titlePane.add(Box.createRigidArea(new Dimension(20, 20)));

        //pane containing labels is between two glue objects for centering
        topPane.add(Box.createHorizontalGlue());
        topPane.add(titlePane);
        topPane.add(Box.createHorizontalGlue());
        add(topPane, BorderLayout.NORTH);

        //numbers to show ordering of names will be paired with textfields
        //in individual panes
        JLabel[] numbers = new JLabel[Main.numScores];
        for(int i = 0; i < Main.numScores; i++) {
            numbers[i] = new JLabel("" + (i + 1));
            numbers[i].setFont(textFont);
            numbers[i].setOpaque(false);
            numbers[i].setForeground(Color.WHITE);
        }
        for(int i = 0; i < numbers.length - 1; i++) {
            numbers[i].setPreferredSize(numbers[numbers.length - 1].getPreferredSize());
        }

        JPanel[] numsAndNames = new JPanel[Main.numScores];
        for(int i = 0; i < numsAndNames.length; i++) {
            numsAndNames[i] = new JPanel();
            numsAndNames[i].setOpaque(false);
            numsAndNames[i].setLayout(new BoxLayout(numsAndNames[i], BoxLayout.LINE_AXIS));
            numsAndNames[i].add(numbers[i]);
            numsAndNames[i].add(Box.createRigidArea(new Dimension(15, 15)));
            numsAndNames[i].add(names[i]);
        }

        JPanel namePane = new JPanel();
        namePane.setOpaque(false);
        namePane.setLayout(new BoxLayout(namePane, BoxLayout.PAGE_AXIS));
        for(int i = 0; i < names.length; i++) {
            if(i != 0) namePane.add(Box.createRigidArea(new Dimension(20, 20)));
            namePane.add(numsAndNames[i]);
        }

        JPanel scorePane = new JPanel();
        scorePane.setOpaque(false);
        scorePane.setLayout(new BoxLayout(scorePane, BoxLayout.PAGE_AXIS));
        for(int i = 0; i < scores.length; i++) {
            if(i != 0) scorePane.add(Box.createRigidArea(new Dimension(20, 20)));
            scorePane.add(scores[i]);
        }

        JPanel contentPane = new JPanel();
        contentPane.setOpaque(false);
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.LINE_AXIS));
        contentPane.add(Box.createRigidArea(new Dimension(50, 50)));
        contentPane.add(namePane);
        contentPane.add(Box.createRigidArea(new Dimension(100, 100)));
        contentPane.add(scorePane);
        contentPane.add(Box.createRigidArea(new Dimension(50, 50)));
        add(contentPane);

        JPanel bottomPane = new JPanel();
        bottomPane.setOpaque(false);
        bottomPane.setLayout(new BoxLayout(bottomPane, BoxLayout.LINE_AXIS));

        JPanel buttonPane = new JPanel();
        buttonPane.setOpaque(false);
        buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.PAGE_AXIS));
        buttonPane.add(Box.createRigidArea(new Dimension(5, 5)));
        quit = new ImageButton(quitImage, "Quit", 312, 50);
        buttonPane.add(quit);
        bottomPane.add(Box.createHorizontalGlue());
        bottomPane.add(buttonPane);
        bottomPane.add(Box.createHorizontalGlue());
        add(bottomPane, BorderLayout.SOUTH);
    }

    public void init() {
        for(int i = 0; i < names.length; i++) {
            names[i].addActionListener(this);
        }
        quit.addActionListener(this);
    }

    public boolean evaluateScore(int score) {
        //find position for new score
        int position = scores.length;
        while(score > Integer.parseInt(scores[position - 1].getText())) {
            position--;
            if(position == 0) break;
        }

        if(position == scores.length) return false;

        //move down scores to make room for new score at its position
        for(int i = Main.numScores - 1; i >= position; i--) {
            //last score has nowhere to move down too, gets deleted
            if(i == Main.numScores - 1) continue;

            scores[i + 1].setText(scores[i].getText());
            names[i + 1].setText(names[i].getText());
        }

        scores[position].setText("" + score);
        names[position].setText("");
        names[position].setFocusable(true);
        names[position].setEditable(true);
        names[position].setOpaque(true);
        names[position].requestFocusInWindow();


        if(Integer.parseInt(Main.highestScore) < Integer.parseInt(scores[0].getText()) )
            Main.highestScore = scores[0].getText();
        return true;
    }

    public void release() {
        for(int i = 0; i < names.length; i++) {
            if(names[i].getText().equals("")) names[i].setText("Anonymous");
            names[i].setEditable(false);
            names[i].setOpaque(false);
            names[i].setFocusable(false);
        }
    }

    public void setFromStart(boolean b) {
        fromStart = b;
    }

    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("Quit")) {
            release();
            parent.remove(this);

            if(fromStart) parent.add(parent.mainMenu);
            else parent.add(parent.pauseMenu);

            fromStart = true;

            parent.validate();
            parent.repaint();
        }

        else {
            int index = -1;
            try {
                index = Integer.parseInt(e.getActionCommand());
            }
            catch(NumberFormatException f) {
                return;
            }

            if(index < 0 || index >= names.length) return;

            names[index].setEditable(false);
            if(names[index].getText().equals("")) names[index].setText("Anonymous");
            names[index].setOpaque(false);
            names[index].setFocusable(false);

            String[] nameStr = new String[Main.numScores];
            int[] scoreStr = new int[Main.numScores];

            for(int i = 0; i < Main.numScores; i++) {
                nameStr[i] = names[i].getText();
                scoreStr[i] = Integer.parseInt(scores[i].getText());
            }
            parent.xio.write(nameStr, scoreStr);
         }
    }

    @Override
    public void paintComponent(Graphics g) {
        if(image != null) g.drawImage(image, 0, 0, null);
    }


}
